﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;

namespace Midas.Tools
{
    public static class _steamAPI
    {
        private static JavaScriptSerializer serializer = new JavaScriptSerializer();
        private static string API_KEY = "C0116375B4D643AA7B4DF80CAB3B90E2";
        private static string baseURL = "http://api.steampowered.com/";

        /// <summary>
        /// this will never be called.
        /// its settings for the class
        /// </summary>
        static _steamAPI()
        {
            serializer.MaxJsonLength = int.MaxValue;
        }

        /// <summary>
        /// This gets all the upcoming tournament matches
        /// not the live games. (at least i think so XD)
        /// </summary>
        /// <returns></returns>
        public static LeagueMatches getLeagueMatches()
        {
            string URL = baseURL;
            URL += "IDOTA2Match_570/GetScheduledLeagueGames/v1/";
            URL += "?key=" + API_KEY;

            return serializer.Deserialize<LeagueMatches>(CallService(URL));
        }

        /// <summary>
        /// this gets a list of items owned by a dota player
        /// </summary>
        /// <param name="SteamID">steam id of the player</param>
        /// <returns>list of items</returns>
        public static PlayerItems getPlayerItems(string SteamID)
        {
            string URL = baseURL;
            URL += "IEconItems_570/GetPlayerItems/v0001/";
            URL += "?key=" + API_KEY;
            URL += "&SteamID=" + SteamID;

            return serializer.Deserialize<PlayerItems>(CallService(URL));
        }

        /// <summary>
        /// gets latest wording used by steam/dota
        /// This could be used if we want to make multiple languages
        /// </summary>
        /// <returns></returns>
        public static GameSchema getGameSchema()
        {
            string URL = baseURL;
            URL += "IEconItems_570/GetSchema/v0001/";
            URL += "?key=" + API_KEY;
            URL += "&language=en";

            return serializer.Deserialize<GameSchema>(CallService(URL));
        }

        /// <summary>
        /// gets steam profile data for 1 or more steam ID's
        /// </summary>
        /// <param name="SteamIDs">array of steam ID's</param>
        /// <returns></returns>
        public static SteamProfileData getSteamProfileData(params string[] SteamID)
        {
            string URL = baseURL;
            URL += "ISteamUser/GetPlayerSummaries/v0002/";
            URL += "?key=" + API_KEY;
            URL += "&steamids=" + string.Join(",", SteamID);

            return serializer.Deserialize<SteamProfileData>(CallService(URL));
        }

        /// <summary>
        /// generic method used to get data from steam/dota
        /// </summary>
        /// <param name="input">url to info</param>
        /// <returns>json string data</returns>
        private static string CallService(string input)
        {
            using (WebClient client = new WebClient())
            {
                return client.DownloadString(input);
            }
        }
    }
}